package com.tojabo.piano;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.tojabo.util.SystemUtil;

public class KeyboardPanel {

	private JPanel thisPanel = new JPanel();
	
	private JButton do_but;
	private JButton re_but;
	private JButton mi_but;
	
	private JButton fa_but;	
	private JButton so_but;
	private JButton la_but;
	private JButton si_but;
	
	private JLabel do_lab;
	private JLabel re_lab;
	private JLabel mi_lab;
	
	private JLabel fa_lab;
	private JLabel so_lab;
	private JLabel la_lab;
	private JLabel si_lab;
	
	private int position_x=0;
	private int position_y=10;
	private int width = 450;
	private int height = 250;
	
	private ImageIcon white_down = SystemUtil.createImageIcon("white_down.png");
	private ImageIcon white_up = SystemUtil.createImageIcon("white_up.png");
	
	private int standard_do; //标准音
	
	private List<JButton> butlist = new ArrayList<JButton>();
	
	public KeyboardPanel(int sound_do){
		standard_do = sound_do-1;
		thisPanel.setLayout(null);
		
		do_but = new JButton(white_up);
		re_but = new JButton(white_up);
		mi_but = new JButton(white_up);
		
		fa_but = new JButton(white_up);
		so_but = new JButton(white_up);
		la_but = new JButton(white_up);
		si_but = new JButton(white_up);
		
		butlist.add(do_but);
		butlist.add(re_but);
		butlist.add(mi_but);
		
		butlist.add(fa_but);
		butlist.add(so_but);
		butlist.add(la_but);
		butlist.add(si_but);
		
		do_lab = new JLabel("1");
		re_lab = new JLabel("2");
		mi_lab = new JLabel("3");
		
		fa_lab = new JLabel("4");
		so_lab = new JLabel("5");
		la_lab = new JLabel("6");
		si_lab = new JLabel("7");
		
		init();
		thisPanel.add(do_but);
		thisPanel.add(re_but);
		thisPanel.add(mi_but);
		
		thisPanel.add(fa_but);
		thisPanel.add(so_but);
		thisPanel.add(la_but);
		thisPanel.add(si_but);
		
		thisPanel.add(do_lab);
		thisPanel.add(re_lab);
		thisPanel.add(mi_lab);
		
		thisPanel.add(fa_lab);
		thisPanel.add(so_lab);
		thisPanel.add(la_lab);
		thisPanel.add(si_lab);
	}
	
	private void init() {
		thisPanel.setBackground(Color.WHITE);
		do_but.setBounds(position_x,position_y,60,230);
		re_but.setBounds(position_x+60,position_y,60,230);
		mi_but.setBounds(position_x+120,position_y,60,230);
		
		fa_but.setBounds(position_x+180,position_y,60,230);
		so_but.setBounds(position_x+240,position_y,60,230);
		la_but.setBounds(position_x+300,position_y,60,230);
		si_but.setBounds(position_x+360,position_y,60,230);
		
		do_lab.setBounds(position_x+30,position_y+240,10,20);
		re_lab.setBounds(position_x+90,position_y+240,10,20);
		mi_lab.setBounds(position_x+150,position_y+240,10,20);
		fa_lab.setBounds(position_x+210,position_y+240,10,20);
		so_lab.setBounds(position_x+270,position_y+240,10,20);
		la_lab.setBounds(position_x+330,position_y+240,10,20);
		si_lab.setBounds(position_x+390,position_y+240,10,20);
		
		do_but.addMouseListener(new MouseAdapter(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				do_but.setIcon(white_up);				
			}
			@Override
			public void mousePressed(MouseEvent e) {
				do_but.setIcon(white_down);
				int temp_index = standard_do;				
				SystemUtil.play_Action(temp_index);
			}
		});
		
		re_but.addMouseListener(new MouseAdapter(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				re_but.setIcon(white_up);				
			}
			@Override
			public void mousePressed(MouseEvent e) {
				re_but.setIcon(white_down);		
				int temp_index = standard_do+1;
				SystemUtil.play_Action(temp_index);
			}
		});
		mi_but.addMouseListener(new MouseAdapter(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				mi_but.setIcon(white_up);				
			}
			@Override
			public void mousePressed(MouseEvent e) {
				mi_but.setIcon(white_down);				
				SystemUtil.play_Action(standard_do+2);
			}
		});
		
		fa_but.addMouseListener(new MouseAdapter(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				fa_but.setIcon(white_up);				
			}
			@Override
			public void mousePressed(MouseEvent e) {
				fa_but.setIcon(white_down);				
				SystemUtil.play_Action(standard_do+3);
			}
		});
		so_but.addMouseListener(new MouseAdapter(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				so_but.setIcon(white_up);				
			}
			@Override
			public void mousePressed(MouseEvent e) {
				so_but.setIcon(white_down);				
				SystemUtil.play_Action(standard_do+4);
			}
		});
		
		la_but.addMouseListener(new MouseAdapter(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				la_but.setIcon(white_up);				
			}
			@Override
			public void mousePressed(MouseEvent e) {
				la_but.setIcon(white_down);				
				SystemUtil.play_Action(standard_do+5);
			}
		});
		si_but.addMouseListener(new MouseAdapter(){			
			@Override
			public void mouseReleased(MouseEvent e) {
				si_but.setIcon(white_up);				
			}
			@Override
			public void mousePressed(MouseEvent e) {
				si_but.setIcon(white_down);				
				SystemUtil.play_Action(standard_do+6);
			}
		});
	}
	
		
	public JPanel getPanel(){
		return thisPanel;
	}
	
	public void changeButImg(int but_index,int but_status){
		JButton but=getButtonByIndex(but_index);
		if(but_status==1){
			but.setIcon(white_down);
		}else{
			but.setIcon(white_up);	
		}
	}
	
	private JButton getButtonByIndex(int index){
		return butlist.get(index);
	}
	
}
